package com.angysnail.guava.event;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

public class EventBusTest {
	public static void main(String[] args) {
		// the event1 is handled:type1 and type=1
		// the event is handled:1
		// the event2 is handled:type2 and type=2
		// the event is handled:2
		test();
	}

	private static void test() {
		EventBus bus = new EventBus();
		bus.register(new Handler());
		bus.post(new EventType1("type1"));
		bus.post(new EventType2("type2"));
	}
}

class Handler {
	@Subscribe
	private void handleEventType(EventType1 et1) {
		System.out.println(String.format("the event1 is handled:%s and type=%d", et1.message, et1.type));
	}

	@Subscribe
	private void handleEventType(EventType2 et2) {
		System.out.println(String.format("the event2 is handled:%s and type=%d", et2.message, et2.type));
	}

	@Subscribe
	private void handleEventType(EventType et) {
		System.out.println(String.format("the event is handled:%d", et.type));
	}
}

class EventType {
	public int type = 0;
}

class EventType1 extends EventType {
	EventType1(String a) {
		this.message = a;
		this.type = 1;
	}

	public String message;
}

class EventType2 extends EventType {
	EventType2(String a) {
		this.message = a;
		this.type = 2;
	}

	public String message;
}